//
//  InviteViewController.m
//  LoveAttention
//
//  Created by 鞠超 on 15/12/30.
//  Copyright © 2015年 JC. All rights reserved.
//

#import "InviteViewController.h"
#import "InviteTableViewCell.h"
#import "NextInviteViewController.h"

#import "DataProvider.h"
#import "UMSocial.h"
#import "UMSocialSnsService.h"
#import "SVProgressHUD.h"
#import "UIImageView+WebCache.h"
#import "Invite_Model.h"
#define umeng_app_key @"557e958167e58e0b720041ff"

@interface InviteViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, strong) UIImageView * image;

@property (nonatomic, strong) UILabel * name;

@property (nonatomic, strong) UILabel * number_inviteSum;

@property (nonatomic, strong) UILabel * number_inviteP;

@property (nonatomic, strong) UILabel * number_refuse;

//数据
@property (nonatomic, strong) NSMutableArray * arr_data;

@end

@implementation InviteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navigationController];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - navigationController
- (void)p_navigationController
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.navigationItem.title = @"我的邀请";
    UIButton * btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [btn setBackgroundImage:[UIImage imageNamed:@"返回"] forState:(UIControlStateNormal)];
        btn.frame = CGRectMake(10, 20, 25, 30);
    [btn addTarget:self action:@selector(leftBarButtonItemAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(rightBarButtonItemAction:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self p_data];
}

//点击事件(左)
- (void)leftBarButtonItemAction:(UIBarButtonItem *)bar
{
    [self.navigationController popViewControllerAnimated:YES];
}

//点击事件(右)
- (void)rightBarButtonItemAction:(UIBarButtonItem *)bar
{
#warning 添加用户,用第三方，好好看看研究下
//    NSLog(@"添加用户");
    
    //分享爱关注
    NSString *shareText = [NSString stringWithFormat:@"输入邀请码，快来加入爱关注，享受生活的乐趣吧"];             //分享内嵌文字
    UIImage *shareImage = [UIImage imageNamed:@"icon_for_attention"];          //分享内嵌图片
    NSArray* snsList=    [NSArray arrayWithObjects:UMShareToQQ,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToSms,nil];
    //调用快速分享接口
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:umeng_app_key
                                      shareText:shareText
                                     shareImage:shareImage
                                shareToSnsNames:snsList
                                       delegate:nil];
}

#pragma mark - 布局
- (void)p_setupView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self p_tableHeadView];
    
    [self.view addSubview:self.tableView];
    //注册
    [self.tableView registerClass:[InviteTableViewCell class] forCellReuseIdentifier:@"cell_invite"];
}

#pragma mark - tableView代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr_data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InviteTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_invite" forIndexPath:indexPath];
    
    if(self.arr_data.count != 0)
    {
        Invite_Model * model = self.arr_data[indexPath.row];
        
        NSString *url = [NSString stringWithFormat:@"%@upload/%@",Url,model.headportain];
        [cell.image sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"头像"]];
        
        cell.name.text = [NSString stringWithFormat:@"%@",model.name];
        
        if([model.sign length] == 0)
        {
            cell.detail.text = [NSString stringWithFormat:@"未设置"];
        }
        else
        {
            cell.detail.text = [NSString stringWithFormat:@"%@",model.sign];
        }
        
        if([model.intime length] == 0)
        {
            
        }
        else
        {
            cell.date.text = [NSString stringWithFormat:@"%@",model.intime];
        }
    }
    
    
    
    return cell;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(self.arr_data.count != 0)
    {
        Invite_Model * model = self.arr_data[indexPath.row];
        
        NextInviteViewController * nextInviteViewController = [[NextInviteViewController alloc] init];
        nextInviteViewController.memberid = model.memberid;
        
        [self showViewController:nextInviteViewController sender:nil];
    }
}


#pragma mark - tableHeadView
- (void)p_tableHeadView
{
    UIView * view_tableHead = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 155)];
    view_tableHead.backgroundColor = [UIColor whiteColor];
    
    self.image = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 25, 17, 50, 50)];
    self.image.image = [UIImage imageNamed:@"头像"];
    self.image.layer.cornerRadius = 25;
    [view_tableHead addSubview:self.image];
    
    self.name = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(self.image.frame) + 5, self.view.frame.size.width - 60, 22)];
    self.name.text = @"为搜哦附近";
    self.name.textAlignment = NSTextAlignmentCenter;
    self.name.font = [UIFont systemFontOfSize:15];
    [view_tableHead addSubview:self.name];
    
    UIView * view_1 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.name.frame) + 5, self.view.frame.size.width, 1)];
    view_1.backgroundColor = [UIColor grayColor];
    [view_tableHead addSubview:view_1];
    
    CGFloat x_distance = self.view.frame.size.width / 2;
    
    self.number_inviteSum = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(view_1.frame) + 10, x_distance - 1, 17)];
    self.number_inviteSum.textAlignment = NSTextAlignmentCenter;
    self.number_inviteSum.text = @"200人";
    self.number_inviteSum.font = [UIFont systemFontOfSize:13];
    [view_tableHead addSubview:self.number_inviteSum];
    
    UILabel * lable_invite1 = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.number_inviteSum.frame) + 3, x_distance - 1, 17)];
    lable_invite1.textAlignment = NSTextAlignmentCenter;
    lable_invite1.text = @"直接邀请人数";
    lable_invite1.font = [UIFont systemFontOfSize:13];
    [view_tableHead addSubview:lable_invite1];
    
    UIView * view_invite1 = [[UIView alloc] initWithFrame:CGRectMake(x_distance - 1, CGRectGetMaxY(view_1.frame) + 3, 1, 47)];
    view_invite1.backgroundColor = [UIColor grayColor];
    [view_tableHead addSubview:view_invite1];
    
    UIView * view_2 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(view_invite1.frame) + 3, self.view.frame.size.width, 1)];
    view_2.backgroundColor = [UIColor grayColor];
    [view_tableHead addSubview:view_2];
    
    
    self.number_inviteP = [[UILabel alloc] initWithFrame:CGRectMake(x_distance, CGRectGetMaxY(view_1.frame) + 10, x_distance - 1, 17)];
    self.number_inviteP.textAlignment = NSTextAlignmentCenter;
    self.number_inviteP.text = @"200人";
    self.number_inviteP.font = [UIFont systemFontOfSize:13];
    [view_tableHead addSubview:self.number_inviteP];
    
    UILabel * lable_invite2 = [[UILabel alloc] initWithFrame:CGRectMake(x_distance, CGRectGetMaxY(self.number_inviteP.frame) + 3, x_distance - 1, 17)];
    lable_invite2.textAlignment = NSTextAlignmentCenter;
    lable_invite2.text = @"间接邀请人数";
    lable_invite2.font = [UIFont systemFontOfSize:13];
    [view_tableHead addSubview:lable_invite2];
    
    UIView * view_invite2 = [[UIView alloc] initWithFrame:CGRectMake(x_distance * 2 - 1, CGRectGetMaxY(view_1.frame) + 3, 1, 47)];
    view_invite2.backgroundColor = [UIColor grayColor];
    [view_tableHead addSubview:view_invite2];
//    
//    self.number_refuse = [[UILabel alloc] initWithFrame:CGRectMake(x_distance * 2, CGRectGetMaxY(view_1.frame) + 10, x_distance - 1, 17)];
//    self.number_refuse.textAlignment = NSTextAlignmentCenter;
//    self.number_refuse.text = @"200人";
//    self.number_refuse.font = [UIFont systemFontOfSize:13];
//    [view_tableHead addSubview:self.number_refuse];
//    
//    UILabel * lable_invite3 = [[UILabel alloc] initWithFrame:CGRectMake(x_distance * 2, CGRectGetMaxY(self.number_refuse.frame) + 3, x_distance - 1, 17)];
//    lable_invite3.textAlignment = NSTextAlignmentCenter;
//    lable_invite3.text = @"邀请拒绝人数";
//    lable_invite3.font = [UIFont systemFontOfSize:13];
//    [view_tableHead addSubview:lable_invite3];
    NSUserDefaults * userdefault=[NSUserDefaults standardUserDefaults];
    
    self.name.text = [NSString stringWithFormat:@"%@",[userdefault objectForKey:@"name"]];
    
    NSString *url = [NSString stringWithFormat:@"%@upload/%@",Url,[userdefault objectForKey:@"imgpath"]];
    [self.image sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"头像"]];
    
    self.tableView.tableHeaderView = view_tableHead;
}

#pragma mark - 加数据
- (void)p_data
{
    DataProvider * dataprovider = [[DataProvider alloc] init];
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [dataprovider setDelegateObject:self setBackFunctionName:@"invite:"];
    
    [dataprovider inviteWithMemberid:[defaults objectForKey:@"memberid"]];
}

- (void)invite:(id )dict
{
    NSLog(@"%@",dict);
    
    self.arr_data = nil;
    
    if ([dict[@"status"][@"succeed"] intValue] == 1) {
        @try
        {
            self.number_inviteSum.text = [NSString stringWithFormat:@"%@",dict[@"data"][@"directInvitation"]];
            self.number_inviteP.text = [NSString stringWithFormat:@"%@",dict[@"data"][@"indirectInvitation"]];
            
            
            for (NSDictionary * dic in dict[@"data"][@"invitationList"])
            {
                Invite_Model * model = [[Invite_Model alloc] init];
                
                [model setValuesForKeysWithDictionary:dic];
                
                [self.arr_data addObject:model];
            }
        }
        @catch (NSException *exception) {
            
        }
        @finally {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                //刷新tableView(记住,要更新放在主线程中)
                
                [self.tableView reloadData];
            });
        }
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:dict[@"status"][@"errdesc"] maskType:SVProgressHUDMaskTypeBlack];
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)arr_data
{
    if(_arr_data == nil)
    {
        self.arr_data = [NSMutableArray array];
    }
    return _arr_data;
}



@end
